package com.example.hotelsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.Role;
import com.example.hotelsystem.entity.TDict;
import com.example.hotelsystem.entity.User;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.*;
import com.example.hotelsystem.vo.dict.DictSearchVO;
import com.example.hotelsystem.vo.list.ListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-03-15
 */
@RestController
@RequestMapping("/hotelsystem/tDict")
@Api(tags = "(下拉菜单)字典数据获取表")
public class TDictController {
    @Autowired
    private TDictService dictService;
    @Autowired
    private FloorService floorService;
    @Autowired
    private RoomTypeService roomTypeService;
    @Autowired
    private HotelNameService hotelNameService;
    @Autowired
    private CityService cityService;
    @Autowired
    private RoleService roleService;

    @PostMapping("/dictList")
    @ApiOperation("获得系统字典列表")
    public Result dictList(DictSearchVO dictSearchVO){
        try{
            QueryWrapper<TDict> queryWrapper = new QueryWrapper<>();
            if(dictSearchVO.getDictName()!=null){
                queryWrapper.like("dict_name",dictSearchVO.getDictName());
            }
            if(dictSearchVO.getDictText()!=null){
                queryWrapper.like("dict_text",dictSearchVO.getDictText());
            }
            Page page = new Page(dictSearchVO.getCurrentPage(), dictSearchVO.getPageSize());
            IPage<TDict> dicts= dictService.page(page,queryWrapper);
            return Result.success(dicts);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增字典")
    public Result addDict(TDict tDict){
        try{
            boolean isAdd = dictService.save(tDict);
            if(isAdd){
                return Result.success("操作成功");
            }
            return Result.error("操作失败");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/mod")
    @ApiOperation("修改字典")
    public Result modDict(TDict tDict){
        try {
            dictService.updateById(tDict);
            return Result.success("操作成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/view")
    @ApiOperation("字典详情")
    public Result viewDict(Long dictId){
        try {
            TDict tDict = dictService.getById(dictId);
            return Result.success(tDict);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/listByDictName")
    @ApiOperation("通过dictName获取字典数据列表")
    public Result listByDictName(String dictName){
        try{
            QueryWrapper<TDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_name",dictName);
            List<TDict> list = dictService.list(queryWrapper);
            return Result.success(list);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/getDictText")
    @ApiOperation("通过dictName和dictKey获取字典数据名称")
    public Result getDictText(String dictName,Integer dictKey){
        try{
            QueryWrapper<TDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_key",dictName);
            queryWrapper.eq("dict_name",dictName);
            TDict tDict = dictService.getOne(queryWrapper);
            if(tDict==null){
                return Result.error("请检查字典key与字典name是否正确");
            }
            return Result.success(tDict);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @PostMapping("/floorlist")
    @ApiOperation("楼层下拉菜单")
    public Result floorList(){
        try{
            List<ListVO> list = floorService.floorListVO();
            return Result.success(list);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/roomTypelist")
    @ApiOperation("房间类型下拉菜单")
    public Result roomTypelist(){
        try{
            List<ListVO> list = roomTypeService.RoomTypeListVO();
            return Result.success(list);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/hotellist")
    @ApiOperation("酒店下拉菜单")
    public Result hotellist(Long cityId){
        try{
            List<ListVO> list = hotelNameService.hotelListVO(cityId);
            return Result.success(list);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/citylist")
    @ApiOperation("城市下拉菜单")
    public Result citylist(){
        try{
            List<ListVO> list = cityService.cityListVO();
            return Result.success(list);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/rolelist")
    @ApiOperation("角色下拉菜单")
    public Result rolelist(){
        try{
            List<Role> list = roleService.list();
            return Result.success(list);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

//    @GetMapping("/QrCode")
//    public void get(HttpServletResponse response, Long orderId) throws Exception {
//        int width = 200;
//        int height = 200;
//        String format = "png";
//        String content = orderId.toString();
//        ServletOutputStream out = response.getOutputStream();
//        Map<EncodeHintType,Object> config = new HashMap<>();
//        config.put(EncodeHintType.CHARACTER_SET,"UTF-8");
//        config.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//        config.put(EncodeHintType.MARGIN, 0);
//        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,config);
//        MatrixToImageWriter.writeToStream(bitMatrix,format,out);
//        System.out.println("二维码生成完毕，已经输出到页面中。");
//    }
}

